package com.sanjishu.enterlab.electricity.sale.controller.manage;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.*;
import com.sanjishu.enterlab.electricity.sale.service.*;
import com.sanjishu.enterlab.electricity.sale.shiro.ShiroAdmin;
import com.sanjishu.enterlab.electricity.sale.utils.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("manage/companyInfo")
public class CompanyManageController {
    @Autowired
    private CompanyManageService companyManageService;
    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private CityService cityService;
    @Autowired
    private ContractSettingService contractSettingService;
    @Autowired
    private DetailInfoService detailInfoService;

    @RequestMapping(value = "newCompany", method = RequestMethod.GET)
    public String newCompany(Integer page, Model model) {
        Page<CompanyInfo> companyInfoPage = companyManageService.findNewCompanyByPage(1);
        model.addAttribute("pageData", companyInfoPage);
        return "manage/companies/index";
    }

    @RequestMapping(value = "newContract", method = RequestMethod.GET)
    public String newContract(Integer page, Model model) {
        Page<ContractInfo> contractInfoPage = contractInfoService.newContractList(1);
        model.addAttribute("pageData", contractInfoPage);
        return "manage/contracts/new";
    }

    @RequestMapping(value = "contracts", method = RequestMethod.GET)
    public String contracts(Integer page, Model model) {
        Page<ContractInfo> contractInfoPage = contractInfoService.contractList(1);
        model.addAttribute("pageData", contractInfoPage);
        return "manage/contracts/list";
    }

    @RequestMapping(value = "edit/{companyId}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer companyId, Model model) {
        CompanyInfo companyInfo = companyInfoService.findById(companyId);
        DetailInfo detailInfo = companyInfoService.findByCompanyId(companyId);
        if (companyInfo != null) {
            City city = cityService.getAreaAndCity(companyInfo.getCityId());
            model.addAttribute("city", city);
            if (city != null) {
                List<City> areaList = null;
                if (city.getParentId() == 1) {
                    areaList = cityService.getArea(city.getId());
                } else {
                    areaList = cityService.getArea(city.getParentId());
                }
                model.addAttribute("areaList", areaList);
            }
        }
        ContractInfo contractInfo = contractInfoService.findByCompanyAndTime(companyId);
        if (contractInfo != null) {
            ContractSetting contractSetting = contractSettingService.findByContractId(contractInfo.getId());
            model.addAttribute("contractSetting", contractSetting);
        }
        List<City> cityList = cityService.getCityList();
        model.addAttribute("cityList", cityList);
        model.addAttribute("companyInfo", companyInfo);
        model.addAttribute("detailInfo", detailInfo);
        model.addAttribute("contractInfo", contractInfo);
        return "manage/companies/edit";
    }

    @RequestMapping(value = "unValidate", method = RequestMethod.POST)
    @ResponseBody
    public Response unValidate(String str, Integer companyId, Integer contractId) {
        return companyInfoService.unValidate(str, companyId, contractId);
    }

    @RequestMapping(value = "validate", method = RequestMethod.POST)
    @ResponseBody
    public Response validate(Integer companyId, Integer contractId) {
        return companyInfoService.validate(companyId, contractId);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Response update(Integer companyId, Integer contractId, Integer detailId, String companyName,
                           String creditCode, String operName, String address, String elecFor, String contractName,
                           String contractPhone, String wxNum, Integer cityId, String capital, String voltageStep,
                           String elecAccount, String transformerCapacity,
                           String elecConsumption, String detailCategory, String bankName,
                           String bankAccountName, String bankAccount, String contactPhone,
                           String fax, String tradeCycle, String startAt, String endedAt,
                           String description) throws Exception {

        CompanyInfo companyInfo = companyInfoService.updateCompany(companyId, companyName, creditCode,
                operName, address, elecFor, contractName, contractPhone, wxNum, capital,
                null, null, null, null, null, null, null);
        DetailInfo detailInfo = detailInfoService.updateDetailInfo(detailId, companyId, voltageStep,
                elecAccount, transformerCapacity, elecConsumption, cityId, detailCategory,
                bankName, bankAccountName, bankAccount, contactPhone, fax);
        ContractInfo contractInfo = contractInfoService.createContractInfo(companyId, contractId, null,
                elecConsumption, description, null,
                null, null, startAt, endedAt, tradeCycle);
        return Response.buildSuccess("编辑成功。");


    }

}
